{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04d361a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def say_hello():           #函数可以有参可以无参\n",
    "    print('Hello!')\n",
    "def say_hi(a='hello!'): #这个有默认参数，即hello!\n",
    "    print(a)\n",
    "    \n",
    "def greetings(x='Good morning!'):\n",
    "    print(x)\n",
    "\n",
    "say_hello()                 # Hello!\n",
    "say_hi()\n",
    "say_hi('hello world!')\n",
    "greetings()                 # Good morning!\n",
    "greetings(\"What's up!\")     # What's up!\n",
    "a = greetings()             # 返回值是None，输出的‘Good morning!’和a无关，只是因为 greetings()被调用。\n",
    "print(1) #隔开输出结果\n",
    "print(a,'apple',greetings(),'bunana') #可见greetings()函数仍然被调用了。\n",
    "say_hi([1,2,3])\n",
    "greetings((1,2,3))\n",
    "greetings({4,5,6})\n",
    "greetings(dict(zip(list(range(0,10,2)),tuple(range(1,9,3)))))\n",
    "d = dict(zip(list(range(0,10,2)),tuple(range(1,9,3))))\n",
    "def sum(val):\n",
    "    # 因为这个sum函数参数只能是序列，所以加一个断言函数和isinstance（）函数来判断是不是list等类型。断言函数assert（）判断为真则继续，\n",
    "    #为假则跳出并报错。isinstance（）需要两个参数（值，类型）。\n",
    "    assert(isinstance(val,(list,tuple,dict,set))) #val type can't equal numer \n",
    "    sum = 0\n",
    "    for i in val:\n",
    "        sum+=i\n",
    "    return sum \n",
    "a = sum(d) #传进sum的参数不能是整型\n",
    "print(a)\n",
    "b = sum(2) #此处2是整型所以会报错\n",
    "print(b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a26d9f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_a_list(x, y=2, z=3):\t# 默认参数项必须放后面\n",
    "    return [x, y, z]\n",
    "\n",
    "b = create_a_list(1)        \t\t# [1, 2, 3]\n",
    "c = create_a_list(3, 4)     \t\t# [3, 3, 3]\n",
    "d = create_a_list(6, 7, 8)  \t    # [6, 7, 8]\n",
    "print(b,c,d,type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92e3e84d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def erjinzhi(a):\n",
    "    s = ''\n",
    "    #1 #0b1 0\n",
    "    #2 #0b10 1\n",
    "    #4 #0b100 1\n",
    "    # a = ob110\n",
    "    b = 1\n",
    "    while b < 2**31:\n",
    "        temp = a & b\n",
    "        if temp == 0:\n",
    "            s += \"0\"\n",
    "        else:\n",
    "            s += \"1\"\n",
    "        b = b << 1\n",
    "    #print(s, \"before\")\n",
    "    s = s[::-1]\n",
    "    #print(s)\n",
    "    for idx,val in enumerate(s):\n",
    "        if val == \"1\":\n",
    "            break\n",
    "    s = s[idx:]\n",
    "    #print(s)\n",
    "    s = \"0b\" + s\n",
    "    return s\n",
    "        \n",
    "# -2**31 ~ 0   \n",
    "# 0 ~ 2**31\n",
    "# 0 ~ 2**32\n",
    "print(erjinzhi(0),\"baby yang\")\n",
    "print(bin(0),\"bin\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b6b0188",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "def erjinzhi2(a):\n",
    "    s = ''\n",
    "    b = 1\n",
    "    while b < 2**31:\n",
    "        temp = a&b\n",
    "        if temp == b:\n",
    "            s += '1'\n",
    "        else:\n",
    "            s += '0'\n",
    "        b <<= 1 #大bug，忘记左移就死循环\n",
    "    s = s[::-1]\n",
    "    for idx,val in enumerate(s):\n",
    "        if val == '1':\n",
    "            break\n",
    "    s = s[idx:]\n",
    "    s = '0b'+ s\n",
    "    return s\n",
    "print(erjinzhi2(2)==bin(2))\n",
    "print(erjinzhi2(3333)==bin(3333))\n",
    "print(erjinzhi2(44444)==bin(44444))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "edc3812b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 9) <class 'tuple'>\n",
      "1 8\n",
      "2 8\n",
      "3 8\n",
      "9 8\n"
     ]
    }
   ],
   "source": [
    "def traverse_args(*a,b,c): # *代表可以传入若干元素，这些元素会当做一个tuple,且变参放前面。\n",
    "    print(a, type(a))\n",
    "    for i in a:\n",
    "        print(i,b)\n",
    "\n",
    "traverse_args( 1, 2, 3, 9 ,b = 8,c = 7)              # 依次打印1, 2, 3\n",
    "#traverse_args('A', 'B', 'C', 'D')   # 依次打印A, B, C, D\n",
    "#traverse_args(('A', 'B', 'C', 'D'))   # 依次打印A, B, C, D\n",
    "#traverse_args()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a6d3085",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c07a635e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "231ece96",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
